package com.yuan.studemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuan.studemo.common.Result;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

import com.yuan.studemo.service.IStuscoreService;
import com.yuan.studemo.entity.Stuscore;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 学生成绩 前端控制器
 * </p>
 *
 * @author Yuan
 * @since 2022-05-25
 */
@RestController
@RequestMapping("/stuscore")
public class StuscoreController {

    @Resource
    private IStuscoreService stuscoreService;

    @PostMapping
    public Result save(@RequestBody Stuscore stuscore){
        //新增或者更新
        return Result.success(stuscoreService.saveOrUpdate(stuscore));
    }

    //删除数据
    @DeleteMapping("/{id}")
    public  Result delete(@PathVariable Integer id){
        return Result.success(stuscoreService.removeById(id));
    }

    //批量删除
    @PostMapping("del/batch")
    public  Result deleteBatch(@RequestBody List<Integer> ids){
        return Result.success(stuscoreService.removeBatchByIds(ids));
    }

    //查询所有数据
    @GetMapping("/")
    public Result findAll(){
        return Result.success(stuscoreService.list());
    }

    //根据id查询
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(stuscoreService.getById(id));
    }

    //分页查询
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String name,
                           @RequestParam(defaultValue = "") String stuId,
                           @RequestParam(defaultValue = "") String coursename) {

        QueryWrapper<Stuscore> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        if (!"".equals(name)) {
            queryWrapper.like("name", name);
        }
        if (!"".equals(stuId)) {
            queryWrapper.like("stuId", stuId);
        }
        if (!"".equals(coursename)) {
            queryWrapper.like("coursename", coursename);
        }
        return Result.success(stuscoreService.page(new Page<>(pageNum, pageSize),queryWrapper));
    }

}

